<template>
  <video
    ref="videoPlayer"
    :id="'vrPlayer' + index"
    class="video-js vjs-default-skin video"
    controls
    crossorigin="anonymous"
  >
    <source :src="src" type="video/mp4" />
  </video>
</template>

<script>
import videojs from "video.js";
import "./dist/videojs-vr";
import "video.js/dist/video-js.css";
import "videojs-vr/dist/videojs-vr.css";
export default {
  props: {
    src: {
      type: String,
      default: require("./test.webm"),
    },
    index: {
      type: String,
      default: "0",
    },
  },
  data() {
    return {
      player: null,
      vr: null,
      isPlay: false,
    };
  },
  created() {
    this.$nextTick(() => {
      this.initPlayer();
    });
  },
  mounted() {
    this.$refs.videoPlayer.addEventListener("play", () => {
      this.isPlay = true;
      this.$emit("play");
      console.log("main在播放了");
    });
    this.$refs.videoPlayer.addEventListener("pause", () => {
      this.isPlay = false;
      console.log("main播放暂停了");
    });
  },
  beforeDestroy() {
    // 在组件销毁前，销毁播放器实例
    if (this.player) {
      this.player.dispose();
    }
  },
  methods: {
    playVideo() {
      let bigPlayButton = this.player.player.bigPlayButton;
      if (this.isPlay == false) {
        bigPlayButton.handleClick((e) => {
          console.log("main播放方法被调用", this.isPlay, e);
        });
      }
    },
    initPlayer() {
      // 初始化 Video.js 播放器实例
      var player = videojs(this.$refs.videoPlayer.id);
      // 设置播放器的 mediainfo
      player.mediainfo = player.mediainfo || {};
      player.mediainfo.projection = "360";
      // this.player = player.vr({
      //   projection: 'AUTO',
      //   debug: true,
      //   forceCardboard: false
      // })
    },
  },
};
</script>

<style scoped lang="scss">
.video-js {
  width: 100%;
  height: 100%;
}
</style>
